package com.itheima.controller.backend.order;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
@Api(tags = "体检套餐管理")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private RedisTemplate redisTemplate;


    /*
    * 新增体检套餐
    * */
    @PostMapping("/add")
    @ApiOperation(value = "新增体检套餐")
    public Result add(@RequestBody Setmeal setmeal,@RequestParam List<Integer> checkgroupIds) {
        //将文件名保存到redis集合中
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add("setmeal_save", setmeal.getImg());

        log.info("新增体检套餐:{},{}", setmeal, checkgroupIds);
        setmealService.add(setmeal, checkgroupIds);
        return new Result(true, "新增体检套餐成功", null);
    }

    /*
    * 根据id修改套餐
    * */
    @PostMapping("/edit")
    @ApiOperation(value = "根据id修改套餐")
    public Result edit(@RequestBody Setmeal setmeal,@RequestParam List<Integer> checkGroupIds) {
        log.info("根据id修改套餐:{},{}", setmeal, checkGroupIds);

        //将文件名保存到redis集合中
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add("setmeal_save", setmeal.getImg());

        setmealService.update(setmeal, checkGroupIds);
        return new Result(true, "修改套餐成功", null);
    }

    /*
    * 根据id查询套餐
    * */
    @GetMapping("/findById")
    @ApiOperation(value = "根据id查询套餐")
    public Result findById(Integer id) {
        log.info("根据id查询套餐:{}", id);
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true, "查询套餐成功", setmeal);
    }

    /*
    * 分页查询
    * */
    @PostMapping("/findPage")
    @ApiOperation(value = "分页查询")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("分页查询:{}", queryPageBean);
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

    /*
     * 文件上传
     * */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result upload(@RequestBody MultipartFile imgFile){
        log.info("文件上传：{}",imgFile);

        try {
            //原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            //截取原始文件名的后缀   dfdfdf.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID().toString() + extension;

            //将文件名保存到redis集合中
            SetOperations setOperations = redisTemplate.opsForSet();
            setOperations.add("setmeal_upload", objectName);

            //文件的请求路径
            String filePath = aliOssUtil.upload(imgFile.getBytes(), objectName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }

        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL, null);
    }
}
